/*未分配表格*/
layui.use([ 'form', 'table' ],  function() { //如果只加载一个模块，可以不填数组。如：layui.use('form')
			var table = layui.table; //获取table模块
			var form = layui.form;
			var $ = layui.jquery;
			// 渲染表格
			table.render({
				elem : '#test2',
				url : globalData.server+'public/getUndistributed',
				toolbar : '#toolbarDemo2', //开启头部工具栏
				cellMinWidth: 100,
				defaultToolbar: ['filter', 'exports', 'print', {
					title: '提示',
					layEvent: 'LAYTABLE_TIPS',
					icon: 'layui-icon-tips'
				}],
				where : {
					method : 'getAll',
					stuname : '',
					userid:'',
					stustate : '',
					stuphone:'',
					Ispay : '',
					isValid:'',
					IsReturnVisit : '',
					QQ:'',
					beginstucreatetime : '',
					endstucreatetime:'',
					beginHomeTime : '',
					endHomeTime:''
				},
				method : 'POST',
				cols : [ [ {
					type: 'checkbox',
					fixed: 'left'
				}, {
					field : 'stuid',
					title : '学生ID',
					fixed : 'left',
					align:'center',
					sort : true,
					width : 120,
				}, {
					field : 'stuname',
					title : '学生姓名',
					align:'center',
					width : 90,
				}, {
					field : 'stuage',
					title : '学生年龄',
					align:'center',
					width : 110,
				}, {
					field : 'stugender',
					title : '学生性别',
					templet: '#switchTpl',
					align:'center',
					width : 110,
				}, {
					field : 'lururen',
					title : '录入人',
					align:'center',
					width : 120,
				}, {
					field : 'suoshuzixunshi',
					title : '所属咨询师',
					align:'center',
					width : 120,
				}, {
					field : 'stuphone',
					title : '学生电话号码',
					align:'center',
					width : 180,
				}, {
					field : 'stuacademic',
					title : '学历',
					align:'center',
					width : 180,
				}, {
					field : 'stustate',
					title : '学生状态',
					templet : '#stustate_pl',
					align:'center',
					width : 125,
				},{
					field : 'stucreatetime',
					title : '创建时间',
					templet : "<div>{{layui.util.toDateString(d.stucreatetime, 'yyyy-MM-dd HH:mm:ss')}}</div>", 
					align:'center',
					width : 180,
				}, {
					field : 'sources',
					title : '来源渠道',
					align:'center',
					width : 180,
				}, {
					field : 'sourceurl',
					title : '来源网址',
					align:'center',
					width : 180,
				}, {
					field : 'sourcekeyword',
					title : '来源关键词',
					align:'center',
					width : 120,
				}, {
					field : 'weChat',
					title : '微信号',
					align:'center',
					width : 120,
				}, {
					field : 'qq',
					title : 'QQ号',
					align:'center',
					width : 120,
				}, {
					field : 'stusourcedep',
					title : '来源部门',
					align:'center',
					width : 120,
				}, {
					field : 'IsReport',
					title : '是否报备',
					templet : '#IsReportbaobei',
					align:'center',
					width : 120,
				}, {
					field : 'learnforward',
					title : '课程方向',
					align:'center',
					width : 120,
				}, {
					field : 'IsValid',
					title : '是否有效',
					templet : '#IsValidyouxiao',
					align:'center',
					width : 120,
				}, {
					field : 'mark',
					title : '打分',
					align:'center',
					width : 120,
				}, {
					field : 'IsReturnVisit',
					title : '是否回访',
					templet : '#IsReturnVisithuifang',
					align:'center',
					width : 120,
				}, {
					field : 'firstVisitTime',
					title : '首次回访时间',
					templet : "<div>{{layui.util.toDateString(d.firstVisitTime, 'yyyy-MM-dd HH:mm:ss')}}</div>", 
					align:'center',
					width : 180,
				}, {
					field : 'IsHome',
					title : '是否上门',
					templet : '#IsHomeshangmen',
					align:'center',
					width : 120,
				}, {
					field : 'homeTime',
					title : '上门时间',
					templet : "<div>{{layui.util.toDateString(d.homeTime, 'yyyy-MM-dd HH:mm:ss')}}</div>", 
					align:'center',
					width : 180,
				}, {
					field : 'invalidReason',
					title : '无效原因',
					align:'center',
					width : 120,
				}, {
					field : 'Ispay',
					title : '是否缴费',
					templet : '#Ispayjiaofei',
					align:'center',
					width : 120,
				}, {
					field : 'payTime',
					title : '缴费时间',
					templet : "<div>{{layui.util.toDateString(d.payTime, 'yyyy-MM-dd HH:mm:ss')}}</div>", 
					align:'center',
					width : 180,
				}, {
					field : 'money',
					title : '金额',
					align:'center',
					width : 120,
				}, {
					field : 'IsReturnMoney',
					title : '是否退费',
					templet : '#IsReturnMoneytuifei',
					align:'center',
					width : 120,
				}, {
					field : 'IsInClass',
					title : '是否进班',
					templet : '#IsInClassjinban',
					align:'center',
					width : 120,
				}, {
					field : 'inClassTime',
					title : '进班时间',
					templet : "<div>{{layui.util.toDateString(d.inClassTime, 'yyyy-MM-dd HH:mm:ss')}}</div>", 
					align:'center',
					width : 180,
				}, {
					field : 'inClassContent',
					title : '进班备注',
					align:'center',
					width : 120,
				}, {
					field : 'refundReason',
					title : '退费原因',
					align:'center',
					width : 120,
				}, {
					field : 'depositMoney',
					title : '定金金额',
					align:'center',
					width : 120,
				}, {
					field : 'depositTime',
					title : '定金时间',
					templet : "<div>{{layui.util.toDateString(d.depositTime, 'yyyy-MM-dd HH:mm:ss')}}</div>", 
					align:'center',
					width : 180,
				}, {
					title : '操作',
					fixed : 'right',
					align : 'center',
					toolbar : '#barDemo',
					width : 400
				} ] ],
				id : 'testReload2',
				page : true
			});
			
			
			// 监听搜索操作
	form.on('submit(data-search-btn2)', function(data) {
		//执行搜索重载
			
			$('#search2').on('click', function() {
				var stuname = $("#stuname2").val();
				var stustate = $("#stustate2").val();
				var userid = $("#suoshuzixunshi2").val();
				var stuphone = $("#stuphone2").val();
				var Ispay = $("#Ispay2").val();
				var isValid = $("#isValid2").val();
				var IsReturnVisit = $("#IsReturnVisit2").val();
				var QQ = $("#QQ2").val();
				var beginstucreatetime = $("#beginstucreatetime2").val();
				var endstucreatetime = $("#endstucreatetime2").val();
				var beginHomeTime = $("#beginHomeTime2").val();
				var endHomeTime = $("#endHomeTime2").val();
				//重载
				table.reload('testReload2', {
					where : {
						'stuname' : stuname,
						'stustate':stustate,
						'userid':userid,
						'stuphone':stuphone,
						'Ispay':Ispay,
						'IsValid':isValid,
						'IsReturnVisit':IsReturnVisit,
						'QQ':QQ,
						'beginstucreatetime':beginstucreatetime,
						'endstucreatetime':endstucreatetime,
						'beginHomeTime':beginHomeTime,
						'endHomeTime':endHomeTime
					}
				});
			});
			});
			/*咨询师动态下拉框*/
			$.get(globalData.server + "public/getAllUsers", {
				'page': 1,
				'limit': 9999
			}, function(obj) {
				console.log(obj);
				if(obj.count > 0) {
					for(var i = 0; i < obj.data.length; i++) {
						$("#suoshuzixunshi2").append(new Option(obj.data[i].username,obj.data[i].userid));
					}
				}
				form.render("select");
			}, "json");
			var onelist=[];
			var twolist=[];
			var type=null;
			//监听表格复选框选择
			table.on('checkbox(test2)', function(obj) {
				console.log(obj);
				console.log(obj.data.stuid);
				console.log(obj.type);
				if (obj.type=='one') {
					type=1;
					if (obj.checked==true) {
						onelist.push(parseInt(obj.data.stuid));
					}else{
						twolist.push(parseInt(obj.data.stuid));
					}
					console.log(onelist);
				} else if (obj.type=='all'){
					type=2;
				}
			});
			
			//监听提交按钮
			table.on('tool(test2)', function(obj) {
				var data = obj.data;
				if (obj.event === 'delStudents') {
					layer.confirm('真的删除该学生信息吗?', function(index) {
						console.log(obj.data);
						var url = globalData.server+'public/deleteStudents2';
						$.post(url, {
							"stuid" : obj.data.stuid,
							method : 'delete'
						}, function(data) {
							if (data.count > 0) {
								console.log('删除成功');
								window.location.reload();
							}
						}, "json");
					});
				} else if (obj.event === 'editStudents') {
					console.log(obj.data);
					layer.open({ //打开一个页面
						type : 2, //在当前的页面之上，弹框形式，展示出添加学生的界面
						area : [ '1100px', '450px' ], //页面的宽高
						content : 'Manager_edit.html', //页面所在的位置
						title : '编辑经理管理学生信息',
						fix:false,
						maxmin:true,
						success : function(layero, index) {
							console.log(obj.data);
							var body = layer.getChildFrame('body', index);
							layui.form.render();
							var iframe = layero.find('iframe')[0].contentWindow;
					body.find("#clerk").val(data.clerk);
					body.find("#userids").val(data.userid);
					body.find("#lururen").val(data.lururen);
					body.find("#suoshuzixunshi").val(data.suoshuzixunshi);
							body.contents().find("#stuid").val(obj.data.stuid);
							body.contents().find("#stuname").val(obj.data.stuname);
							body.contents().find("#stuage").val(obj.data.stuage);
							body.contents().find("#stugender2").val(obj.data.stugender);
							body.contents().find("#stuphone").val(obj.data.stuphone);
							body.contents().find("#stuacademic").val(obj.data.stuacademic);
							body.contents().find("#stustate2").val(obj.data.stustate);
							body.contents().find("#stucreatetime").val(layui.util.toDateString(obj.data.stucreatetime, 'yyyy-MM-dd HH:mm:ss'));
							body.contents().find("#sources").val(obj.data.sources);
							body.contents().find("#sourceurl").val(obj.data.sourceurl);
							body.contents().find("#sourcekeyword").val(obj.data.sourcekeyword);
							body.contents().find("#clerkid").val(obj.data.clerkid);
							body.contents().find("#userid").val(obj.data.userid);
							body.contents().find("#WeChat").val(obj.data.weChat);
							body.contents().find("#QQ").val(obj.data.qq);
							body.contents().find("#stusourcedep").val(obj.data.stusourcedep);
							body.contents().find("#IsReport").val(obj.data.isReport);
							body.contents().find("#learnforward").val(obj.data.learnforward);
							body.contents().find("#IsValid").val(obj.data.isValid);
							body.contents().find("#mark").val(obj.data.mark);
							body.contents().find("#IsReturnVisit").val(obj.data.isReturnVisit);
							body.contents().find("#FirstVisitTime").val(layui.util.toDateString(obj.data.firstVisitTime, 'yyyy-MM-dd HH:mm:ss'));
							body.contents().find("#IsHome").val(obj.data.isHome);
							body.contents().find("#HomeTime").val(layui.util.toDateString(obj.data.HomeTime, 'yyyy-MM-dd HH:mm:ss'));
							body.contents().find("#InvalidReason").val(obj.data.invalidReason);
							body.contents().find("#Ispay").val(obj.data.ispay);
							body.contents().find("#PayTime").val(layui.util.toDateString(obj.data.PayTime, 'yyyy-MM-dd HH:mm:ss'));
							body.contents().find("#Money").val(obj.data.money);
							body.contents().find("#IsReturnMoney").val(obj.data.isReturnMoney);/* 更改下拉框的map返回值大写I .IsReturnMoney*/
							body.contents().find("#IsInClass").val(obj.data.isInClass);
							body.contents().find("#InClassTime").val(layui.util.toDateString(obj.data.inClassTime, 'yyyy-MM-dd HH:mm:ss'));
							body.contents().find("#InClassContent").val(obj.data.inClassContent);
							body.contents().find("#RefundReason").val(obj.data.refundReason);
							body.contents().find("#DepositMoney").val(obj.data.depositMoney);
							body.contents().find("#DepositTime").val(layui.util.toDateString(obj.data.depositTime, 'yyyy-MM-dd HH:mm:ss'));
						
							body.contents().find("#WeChat").val(obj.data.weChat);
							body.contents().find("#QQ").val(obj.data.qq);
							body.contents().find("#InvalidReason").val(obj.data.invalidReason);
							body.contents().find("#Money").val(obj.data.money);
							body.contents().find("#InClassContent").val(obj.data.inClassContent);
							body.contents().find("#RefundReason").val(obj.data.refundReason);
							body.contents().find("#DepositMoney").val(obj.data.depositMoney);
							iframe.layui.form.render('select');
						
						},
						end : function() {
							window.location.reload();
						}
					});
				}else if(obj.event === 'tarck') {
					var index = layer.open({
						title: '网络跟踪 ' + data.stuname+" 同学：",
						type: 2,
						shade: 0.2,
						maxmin: true,
						shadeClose: true,
						area: ['100%', '100%'],
						content: 'TrackStudents.html',
						success: function(layero, index) {
							var body = layer.getChildFrame('body', index);
							body.find("#stuid").val(data.stuid);
						},
						end: function() {
							window.location.reload();
						}
					});
					$(window).on("resize", function() {
						layer.full(index);
					});
				}else if(obj.event === 'sendMsg') {
					var index = layer.open({
						title: '发送消息',
						type: 2,
						shade: 0.2,
						maxmin: true,
						shadeClose: true,
						area : [ '1100px', '450px' ], //页面的宽高
						content: 'Messages_add.html',
						success: function(layero, index) {
							var body = layer.getChildFrame('body', index);
							 body.find("#stuname").val(data.stuname); 
							 body.find("#messtuid").val(data.stuid);
							 body.find("#mesreceiver").val(data.userid); 
							 body.find("#suoshuzixunshi").val(data.suoshuzixunshi);
						},
						end: function() {
							window.location.reload();
						}
					});
					$(window).on("resize", function() {
						layer.full(index);
					});
				} else if(obj.event === 'createTarck') {
					var index = layer.open({
						title: '添加 ' + data.stuname + ' 同学的跟踪信息',
						type: 2,
						shade: 0.2,
						maxmin: true,
						shadeClose: true,
						area: ['100%', '100%'],
						content: 'TrackStudents_add.html',
						success: function(layero, index) {
							var body = layer.getChildFrame('body', index);
							body.find("#stuid").val(data.stuid);
						},
						end: function() {
							window.location.reload();
						}
					});
					$(window).on("resize", function() {
						layer.full(index);
					});
				}
				
			});
			

			//监听行单击事件（双击事件为：rowDouble）
			table.on('rowDouble(test2)', function(obj) {
				var data = obj.data;
				var index = layer.open({
						title:  data.stuname+" 学生详情页面",
						type: 2,
						shade: 0.2,
						maxmin: true,
						shadeClose: true,
						area : [ '1100px', '450px' ], //页面的宽高
						content: 'Students_detail.html',
						success: function(layero, index) {
							console.log(obj.data);
							var body = layer.getChildFrame('body', index);
							layui.form.render();
							var iframe = layero.find('iframe')[0].contentWindow;
							
							body.contents().find("#stuid").val(obj.data.stuid);/* title */
							body.contents().find("#stuname").val(obj.data.stuname);
							body.contents().find("#stuage").val(obj.data.stuage);
							body.contents().find("#stugender").val(obj.data.stugender);
							body.contents().find("#stuphone").val(obj.data.stuphone);
							body.contents().find("#stuacademic").val(obj.data.stuacademic);
							body.contents().find("#stustate").val(obj.data.stustate);
							body.contents().find("#stucreatetime").val(layui.util.toDateString(obj.data.stucreatetime, 'yyyy-MM-dd HH:mm:ss'));
							body.contents().find("#sources").val(obj.data.sources);
							body.contents().find("#sourceurl").val(obj.data.sourceurl);
							body.contents().find("#sourcekeyword").val(obj.data.sourcekeyword);
							body.contents().find("#clerkid").val(obj.data.lururen);
							body.contents().find("#userid").val(obj.data.suoshuzixunshi);
							body.contents().find("#WeChat").val(obj.data.weChat);
							body.contents().find("#QQ").val(obj.data.qq);
							body.contents().find("#stusourcedep").val(obj.data.stusourcedep);
							body.contents().find("#IsReport").val(obj.data.isReport);
							body.contents().find("#learnforward").val(obj.data.learnforward);
							body.contents().find("#IsValid").val(obj.data.isValid);
							body.contents().find("#mark").val(obj.data.mark);
							body.contents().find("#IsReturnVisit").val(obj.data.isReturnVisit);
							body.contents().find("#FirstVisitTime").val(layui.util.toDateString(obj.data.firstVisitTime, 'yyyy-MM-dd HH:mm:ss'));
							body.contents().find("#IsHome").val(obj.data.isHome);
							body.contents().find("#HomeTime").val(layui.util.toDateString(obj.data.homeTime, 'yyyy-MM-dd HH:mm:ss'));
							body.contents().find("#InvalidReason").val(obj.data.invalidReason);
							body.contents().find("#Ispay").val(obj.data.ispay);
							body.contents().find("#PayTime").val(layui.util.toDateString(obj.data.payTime, 'yyyy-MM-dd HH:mm:ss'));
							body.contents().find("#Money").val(obj.data.money);
							body.contents().find("#IsReturnMoney").val(obj.data.isReturnMoney);
							body.contents().find("#IsInClass").val(obj.data.isInClass);
							body.contents().find("#InClassTime").val(layui.util.toDateString(obj.data.inClassTime, 'yyyy-MM-dd HH:mm:ss'));
							body.contents().find("#InClassContent").val(obj.data.inClassContent);
							body.contents().find("#RefundReason").val(obj.data.refundReason);
							body.contents().find("#DepositMoney").val(obj.data.depositMoney);
							body.contents().find("#DepositTime").val(layui.util.toDateString(obj.data.depositTime, 'yyyy-MM-dd HH:mm:ss'));
							iframe.layui.form.render('select');
						},
						end: function() {
						}
					});

				//标注选中样式
				obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
			});
			
			
			
			
			
			/**
			 * toolbar监听事件
			 */
			 table.on('toolbar(test2)', function(obj) {
				if (obj.event==='addStudents') { // 监听添加操作
				
				var index=	layer.open({
						type : 2,
						area : [ '1100px', '500px' ], //页面的宽高
						content : 'students_add.html',
						title : '添加学生信息',
						fix:false,
						maxmin:true,
						end : function() {
							window.location.reload();
						}
					});
				$(window).on("resize", function() {
					layer.full(index);
				});
				}else if (obj.event==='excel') {
					//询问框
					layer.confirm('确定导出表格吗？', {
					  btn: ['确定','取消'] //按钮
					}, function(){
						$.post(globalData.server+"public/excel",{
							"type":type,
							"one":onelist,
							"two":twolist
						},function(res){
							if (res.count>0) {
								 // 假如返回的 res.data 是需要导出的列表数据
				            console.log(res.data);// [{name: 'wang', age: 18, sex: '男'}, {name: 'layui', age: 3, sex: '女'}]
				            // 1. 数组头部新增表头
				            res.data.unshift({stuid: '学生ID',
				            	stuname: '学生姓名',
				            	stuage: '学生年龄',
				            	stugender: '学生性别',
				            	stuphone :'学生电话号码',
				            	stuacademic:'学历',
				            	stustate:'学生状态',
				            	stucreatetime:'创建时间',
				            	sources:'来源渠道',
				            	sourceurl:'来源网址',
				            	sourcekeyword:'来源关键词',
				            	lururen:'录入人',
				            	suoshuzixunshi:'所属咨询师',
				            	weChat:'微信号',
				            	qq:'QQ号',
				            	stusourcedep:'来源部门',
				            	isReport:'是否报备',
				            	learnforward:'课程方向',
				            	isValid:'是否有效',
				            	mark:'打分',
				            	isReturnVisit:'是否回访',
				            	firstVisitTime:'首次回访时间',
				            	isHome:'是否上门',
				            	homeTime:'上门时间',
				            	invalidReason:'无效原因',
				            	ispay:'是否缴费',
				            	payTime:'缴费时间',
				            	money:'金额',
				            	isReturnMoney:'是否退费',
				            	isInClass:'是否进班',
				            	inClassTime:'进班时间',
				            	inClassContent:'进班备注',
				            	refundReason:'退费原因',
				            	depositMoney:'定金金额',
				            	depositTime:'定金时间' });
				            // 2. 如果需要调整顺序，请执行梳理函数
				            var data = LAY_EXCEL.filterExportData(res.data, [
				                'stuid','stuname','stuage','stugender',
				                'stuphone','stuacademic','stustate','stucreatetime',
				                'sources','sourceurl','sourcekeyword','lururen',
				                'suoshuzixunshi','weChat','qq','stusourcedep',
				                'isReport','learnforward','isValid','mark','isReturnVisit',
				                'firstVisitTime','isHome','homeTime','invalidReason','ispay',
				                'payTime','money','isReturnMoney','isInClass','inClassTime',
				                'inClassContent','refundReason','depositMoney','depositTime'
				            ]);
				            // 3. 执行导出函数，系统会弹出弹框
				            LAY_EXCEL.exportExcel({
				                sheet1: data
				            }, '所有学生信息' + layui.util.toDateString(new Date(), 'yyyy年MM月dd日 HH:mm:ss') + '.xlsx', 'xlsx');
							}
						},"json");
					}, function(){
						
					});
				}else if(obj.event === 'consultant') {
					//询问框
					layer.confirm('确定批量修改所属咨询师吗？', {
						btn: ['确定', '取消'] //按钮
					}, function() {
						var index = layer.open({
						title: '进行批量修改所属咨询师',
						type: 2,
						shade: 0.2,
						maxmin: true,
						shadeClose: true,
						area : [ '600px', '500px' ], //页面的宽高
						content: 'ChooseModifyCounselor.html',
						success: function(layero, index) {
							var body = layer.getChildFrame('body', index);
							body.find("#one").val(onelist);
							body.find("#two").val(twolist);
						},
						end: function() {
							window.location.reload();
						}
					});
					}, function() {

					});
				}
			}); 
		});
		layui.use('laydate', function() {
			var laydate = layui.laydate;
			//同时绑定多个时间选择器
			laydate.render({
				elem : '#beginstucreatetime',
				type : 'datetime'
			});
			laydate.render({
				elem : '#endstucreatetime',
				type : 'datetime'
			});
			laydate.render({
				elem : '#beginHomeTime',
				type : 'datetime'
			});
			laydate.render({
				elem : '#endHomeTime',
				type : 'datetime'
			});
		});